/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import DTO.xmlobject.Contacts.Contact;
import DTO.xmlobject.Contacts.Contacts;

/**
 *
 * @author ASUS
 */
public class ContactDAO extends Dataconnection {

    private Contact ReadContactFromDS(ResultSet rs) {
        Contact result = new Contact();
        if (rs != null) {
            try {
                result.setContactID(rs.getLong("ContactID"));
                result.setFullName(rs.getString("Fullname"));
                result.setPhone(rs.getString("Phone"));
                result.setEmail(rs.getString("Email"));
                result.setAddress(rs.getString("Address"));
                result.setIsDelete(rs.getBoolean("IsDelete"));
                result.setCreatedDate(rs.getDate("CreatedDate"));
                result.setCreatedBy(rs.getString("CreatedBy"));
                result.setUpdatedDate(rs.getDate("UpdatedDate"));
                result.setUpdatedBy(rs.getString("UpdatedBy"));
            } catch (SQLException ex) {
                Logger.getLogger(ContactDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

    public Contact getContactById(int id) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spContactGet(?) }");
            proc_stmt.setLong("UserID", id);
            rs = proc_stmt.executeQuery();
            if (rs.next()) {
                return ReadContactFromDS(rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ContactDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Contacts getAllProduct() {
        Contacts result = new Contacts();
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spContactGetList() }");
            rs = proc_stmt.executeQuery();
            while (rs.next()) {
                result.getContact().add(ReadContactFromDS(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ContactDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    public Contact Create(Contact value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spContactInsert(?,?,?,?,?,?,?,?,?,?) }");
            proc_stmt.registerOutParameter("ContactID", java.sql.Types.INTEGER);
            proc_stmt.setString("Fullname", value.getFullName());
            proc_stmt.setString("Phone", value.getPhone());
            proc_stmt.setString("Email", value.getEmail());
            proc_stmt.setString("Address", value.getAddress());
            proc_stmt.setBoolean("IsDelete", value.isIsDelete());
            proc_stmt.setDate("CreatedDate", value.getCreatedDate());
            proc_stmt.setString("CreatedBy", value.getCreatedBy());
            proc_stmt.setDate("UpdatedDate", value.getUpdatedDate());
            proc_stmt.setString("UpdatedBy", value.getUpdatedBy());
            proc_stmt.execute();
            value.setContactID(proc_stmt.getLong("ContactID"));
        } catch (SQLException ex) {
            Logger.getLogger(ContactDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return value;
    }

    public Contact Edit(Contact value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spContactUpdate(?,?,?,?,?,?,?,?,?,?) }");
            proc_stmt.registerOutParameter("ContactID", java.sql.Types.INTEGER);
            proc_stmt.setString("Fullname", value.getFullName());
            proc_stmt.setString("Phone", value.getPhone());
            proc_stmt.setString("Email", value.getEmail());
            proc_stmt.setString("Address", value.getAddress());
            proc_stmt.setBoolean("IsDelete", value.isIsDelete());
            proc_stmt.setDate("CreatedDate", value.getCreatedDate());
            proc_stmt.setString("CreatedBy", value.getCreatedBy());
            proc_stmt.setDate("UpdatedDate", value.getUpdatedDate());
            proc_stmt.setString("UpdatedBy", value.getUpdatedBy());
            proc_stmt.execute();
        } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
     public boolean Delete(int value) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spContactDelete(?) }");
            proc_stmt.setLong("ContactID", value);
            return proc_stmt.execute();
        } catch (SQLException ex) {
            Logger.getLogger(ContactDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
